/*******************************************************************************
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors: SALMON Charles
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 ******************************************************************************/

/**
 * A form to create a new topic
 */
qx.Class.define("bip.authorview.forms.NewTopicForm",
{
  extend : qx.ui.form.Form,
  
  include: [bip.authorview.forms.MControllerForm, qx.locale.MTranslation],
  
  construct : function()
  {
	
    this.base(arguments);
    // Add fields
    this.addGroupHeader("Add a new topic to the survey");
    // IdLocal
    var localSelectfield = new bip.authorview.forms.LocaleSelectBox();
    this.add(localSelectfield, "IdLocal", null, "idLocal");
    // Name
    var nameTextfield = new qx.ui.form.TextField();
    nameTextfield.setRequired(true);
    nameTextfield.setWidth(100);
    this.add(nameTextfield, "Name", null, "name");
    // Description
    var descTextfield = new qx.ui.form.TextField();
    descTextfield.setWidth(200);
    descTextfield.setRequired(true);
    this.add(descTextfield, "Description", null, "description");
    
    // Button
    var addButton = bip.ui.form.PredefinedButtons.newAddButton();
    addButton.addListener("execute",this.__onAddButtonClickHandler,this);
    this.addButton(addButton);
    
  },
  
	/*
	 * ****************************************************************************
	 * MEMBERS
	 * ****************************************************************************
	 */

 	members :
 	{
		
		__onAddButtonClickHandler: function(evt) {
			if (this.validate()) {
				this.getController().updateModel();
				var model = this.getController().getModel();
				model.setIdTopic(0);  // 0 => create model
				bip.Application.setState("running",this.tr("Creating topic, please wait"));
				bip.authorview.SharedServices.getInstance().getTopicService().addListenerOnce("state",function(evt) {
					if (evt.getData().getState() == "ok") {
						bip.Application.setState(evt.getData().getState(),"The topic has been created");
						bip.services.Survey.getInstance().getSurveyTree();
					} else {
						bip.Application.setState(evt.getData().getState(),evt.getData().getModel().getMessage());
					}
				});
				bip.authorview.SharedServices.getInstance().getTopicService().postTopic(model);

			} else {
				bip.Application.setState("error","Please correct the error(s) on this form");
			}
		}
 		
 	},
 	
	/*
	 * ****************************************************************************
	 * DESTRUCTOR
	 * ****************************************************************************
	 */

	destruct : function() {
		this._disposeObjects();
	}

  
});